// pages/AddVedio/AddVedio.js
const db = wx.cloud.database();
let that = this;
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['教学',  '其他'],
    imageArr:['cloud://puzzle-rafting-7gnlcdsq7df989f6.7075-puzzle-rafting-7gnlcdsq7df989f6-1314118931/前端那些装饰图片/yule.jpg',
    'cloud://puzzle-rafting-7gnlcdsq7df989f6.7075-puzzle-rafting-7gnlcdsq7df989f6-1314118931/前端那些装饰图片/qita.jpg'],
    user_Name: '',
    user_url: '',
    user_openid: '',

    title: '', //标题
    description: '', //简介
    vedio: '', //视频路径
    image:'',
    type:'',
    number: 1,
    icon: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let userinfo = wx.getStorageSync('app.globalData.userInfo');
    let openid = wx.getStorageSync('app.globalData.userOpenId')
    this.setData({
      user_openid: openid,
      user_Name: userinfo.nickName,
      user_url: userinfo.avatarUrl
    })
  },

  getTitle: function (e) {
    this.setData({
      tilte: e.detail.value
    })
  },

  getInput: function (e) {
    this.setData({
      description: e.detail.value
    })
  },

  addVedioPath(fileId) {
    var that = this
    console.log(fileId)
    wx.cloud.getTempFileURL({
      fileList: [fileId],
      success: res => {
        that.data.icon = res.fileList[0].tempFileURL
        that.data.vedio=res.fileList[0].tempFileURL
        console.log(that.data.icon)
      },
      fail: console.error
    })
  },

  addVedio: function (e) {
    var that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['video'],
      sourceType: ['album', 'camera'],
      sizeType: ['ccompressed'],
      success(res) {
        console.log("成功", res);
        that.uploadVedio(res.tempFiles[0].tempFilePath)
        that.setData({
          number: res.tempFiles.length + 1
        })
      },
      fail: console.error()
    })
  },

  uploadVedio(FileURL) {
    var that = this
    wx.showLoading({
      title: '上传中...',
    })
    wx.cloud.uploadFile({
      cloudPath: "userVedio/" + Date.now() + '.mp4',
      filePath: FileURL
    }).then(res => {
      that.addVedioPath(res.fileID)
      console.log(res)
      wx.hideLoading()
    })
  },

  deleteVedio: function (e) {
    var that = this
    var index = e.target.dataset.index
    var tempFilePaths = that.data.Filepath
    console.log(tempFilePaths)
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('点击确定了');
          tempFilePaths.splice(index, 1);
        } else if (res.cancel) {
          console.log('点击取消了');
          return false;
        }
        that.setData({
          Filepath: tempFilePaths,
          number: that.data.number - 1
        });
      }
    })
  },

  pickType: function (e) {
    console.log(e)
    this.setData({
      type: this.data.array[e.detail.value],
      image:this.data.imageArr[e.detail.value]
    })
    console.log(this.data.type)
  },

  btnsub: function (res) {
    var that = this
    console.log(res)
    var title=that.data.title
    var description=that.data.description
    var usrn = that.data.user_Name
    var usri = that.data.user_url
    var usrid = that.data.userOpenId
    var vedio=that.data.vedio
    var image=that.data.image
    var type=that.data.type

    if (title != "" && vedio != "") {
      wx.showLoading({
        title: '发布中...',
        mask: true,
        duration: 1000
      }).then(res => {
        db.collection("vedio").add({
          data: {
            userOpenId: usrid,
            user_nickname: usrn,
            user_url: usri,
            title:title,
            description:description,
            vedio:vedio,
            image:image,
            type:type
          }
        }).then(res => {
          wx.hideLoading()
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 1000,
            success: function () {
              console.log(res)
            }
          })
          that.setData({
            usrid: '',
            user_nickname: '',
            user_url: '',
            activityName: '',
            description: '',
            donation_name: '',
            donation_image: '',
            telephoneNumber: '',
            image:image,
            type: '',
            number: 1
          })
          wx.switchTab({
            url: '../home/home'
          })
        }, )
      })
      fail: console.error();
    } else {
      wx.showToast({
        icon: 'none',
        title: '请检查输入的数据是否有误！',
        duration: 1000,
        mask: true,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})